package com.example.combwyaq.visualinterface.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import androidx.appcompat.app.AppCompatActivity;

import com.example.combwyaq.R;
import com.example.combwyaq.db.DaoManager;
import com.example.combwyaq.db.UserEntityDao;
import com.example.combwyaq.entity.UserEntity;

public class ModifyActivity extends AppCompatActivity {

    private ImageView goBack;
    private EditText name;
    private Button comit;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify);
        initView();
        goBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        comit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    String s = name.getText().toString();
                    UserEntity unique = DaoManager.getInstance().getDaoSession().queryBuilder(UserEntity.class)
                            .where(UserEntityDao.Properties.Id.eq(0))
                            .unique();
                    if (unique != null) {
                        unique.setUser_name(s);
                    }
                    DaoManager.getInstance().getDaoSession().update(unique);
                    finish();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

    }

    private void initView() {
        goBack = (ImageView) findViewById(R.id.go_back);
        name = (EditText) findViewById(R.id.name);
        comit = (Button) findViewById(R.id.comit);
    }
}
